package com.visitor.registration.controller;

import com.visitor.registration.dto.ApiResponse;
import com.visitor.registration.dto.VisitorDTO;
import com.visitor.registration.entity.City;
import com.visitor.registration.entity.InterestArea;
import com.visitor.registration.entity.Province;
import com.visitor.registration.entity.Visitor;
import com.visitor.registration.entity.Type;
import com.visitor.registration.service.VisitorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "访客登记管理")
@RestController
@RequestMapping("/api/visitor")
@RequiredArgsConstructor
@CrossOrigin
public class VisitorController {

    @Autowired
    VisitorService visitorService;
    
    @Operation(summary = "提交访客信息")
    @PostMapping("/submit")
    public ApiResponse<Visitor> submitVisitor(@RequestBody VisitorDTO visitorDTO) {
            Visitor visitor = visitorService.saveVisitor(visitorDTO);
            return ApiResponse.success(visitor);
    }
    
    @Operation(summary = "获取所有访客信息")
    @GetMapping("/list")
    public ApiResponse<List<Visitor>> getAllVisitors() {
        try {
            List<Visitor> visitors = visitorService.getAllVisitors();
            return ApiResponse.success(visitors);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取所有省份")
    @GetMapping("/provinces")
    public ApiResponse<List<Province>> getAllProvinces() {
        try {
            List<Province> provinces = visitorService.getAllProvinces();
            return ApiResponse.success(provinces);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @Operation(summary = "根据省份ID获取城市列表")
    @GetMapping("/cities/{provinceId}")
    public ApiResponse<List<City>> getCitiesByProvinceId(
            @Parameter(description = "省份ID") @PathVariable Integer provinceId) {
        try {
            List<City> cities = visitorService.getCitiesByProvinceId(provinceId);
            return ApiResponse.success(cities);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取所有关注领域")
    @GetMapping("/interest-areas")
    public ApiResponse<List<InterestArea>> getAllInterestAreas() {
        try {
            List<InterestArea> interestAreas = visitorService.getAllInterestAreas();
            return ApiResponse.success(interestAreas);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @Operation(summary = "获取所有访客类型")
    @GetMapping("/types")
    public ApiResponse<List<Type>> getAllTypes() {
        try {
            List<Type> types = visitorService.getAllTypes();
            return ApiResponse.success(types);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @Operation(summary = "手动触发生成报表并发送邮件")
    @GetMapping("generate-report")
    public ApiResponse<Void> generateReport() {
        try {
            visitorService.generateReportAndSendMail();
            return ApiResponse.success();
        } catch (Exception e) {
            String message = e.getMessage();
            System.out.println(message);
            return ApiResponse.error(e.getMessage());
        }
    }
} 